package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class addressform extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String street = null;
	private String city = null;
	private String lstate = null;
	private String zip = null;
	private String areaCode = null;
	private String pnumber = null;
	
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getLstate() {
		return lstate;
	}
	public void setLstate(String lstate) {
		this.lstate = lstate;
	}
	public String getZip() {
		return zip;
	}
	public void setZip(String zip) {
		this.zip = zip;
	}
	public String getAreaCode() {
		return areaCode;
	}
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
	public String getPnumber() {
		return pnumber;
	}
	public void setPnumber(String pnumber) {
		this.pnumber = pnumber;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setStreet("");
		setCity("");
		setLstate("");
		setZip("");
		setAreaCode("");
		setPnumber("");
	}
	
	private boolean acInvalid(char[] word)
	{
		boolean invalid = false;
		for(int i = 0; i < word.length; i++ )
		{
			if( (word[i] >= 48) && (word[i] <= 57) )
			{
				invalid = false;
			}
			else
			{
				invalid = true;
				break;
			}
		}
		return invalid;
	}
	
	private boolean pnInvalid(char[] word)
	{
		boolean invalid = false;
		for(int i = 0; i < word.length; i++ )
		{
			if( (word[i] >= 48 && word[i] <= 57) || (word[i] == 32) || (word[i] == 45) )
			{
				invalid = false;
			}
			else
			{
				invalid = true;
				break;
			}
		}
		return invalid;
	}
	
	// check if first name, middle initial, and last name are provided and valid
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		if( street.equals("") )
		{
			errors.add("street", new ActionMessage("error.street.required"));
			setStreet("");
		}
		if( city.equals("") )
		{
			errors.add("city", new ActionMessage("error.city.required"));
			setCity("");
		}
		if( pnumber.equals("") )
		{
			errors.add("pnumber", new ActionMessage("error.pnumber.required"));
			setPnumber("");
		}
		else
		{
			if( pnInvalid(pnumber.toCharArray()) )
			{
				errors.add("pnumber", new ActionMessage("error.pnumber.invalid"));
				setPnumber("");
			}
		}

		if( request.getSession().getAttribute("citizen").equals("United States") || 
				  request.getSession().getAttribute("citizen").equals("United States Virgin Islands") ) {
			if( lstate.equals("") )
			{
				errors.add("lstate", new ActionMessage("error.lstate.required"));
				setLstate("");
			}
			if( areaCode.equals(""))
			{
				errors.add("areaCode", new ActionMessage("error.areaCode.required"));
				setAreaCode("");
			}
			else
			{
				if( acInvalid(areaCode.toCharArray()) )
				{
					errors.add("areaCode", new ActionMessage("error.areaCode.invalid"));
					setAreaCode("");
				}
			}
			if( zip.equals("") )
			{
				errors.add("zip", new ActionMessage("error.zip.required"));
				setZip("");
			}
		}
		else
		{			
			if( areaCode.equals(""))
			{
				errors.add("areaCode", new ActionMessage("error.counCode.required"));
				setAreaCode("");
			}
			else
			{
				if( acInvalid(areaCode.toCharArray()) )
				{
					errors.add("areaCode", new ActionMessage("error.counCode.invalid"));
					setAreaCode("");
				}
			}
		}
		
		return errors;
	}


	
	
}
